// pages/member/member.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    userInfo: {}, // 用户信息
    list:'',
    year:'',
    num:'',
    status:'',
    time:''
  },
  onLoad: function (options) {
    this.getData()
    this.member()
  },
  getData() {
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/memberPrice',
      method:'GET',
      data: {
        // wxapp_id:wx.getStorageSync("user_id"),
        wxapp_id:10001,
        token:wx.getStorageSync("token")
      },
      success: res => {
        this.setData({
          list:res.data.data,
          year:res.data.data[0].value
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  member() {
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/memberTime',
      method:'GET',
      data: {
        wxapp_id:10001,
        token:wx.getStorageSync("token")
      },
      success: res => {
        console.log(res)
        this.setData({
          status:res.data.data.status,
          time:res.data.data.time
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  year(e){
    this.setData({
      year :e.currentTarget.dataset.id,
      num :e.currentTarget.dataset.index
    })
    console.log(this.data.year)
  },
  money(){
    let _this=this;
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/payMember',
      method:'GET',
      data: {
        wxapp_id:10001,
        token:wx.getStorageSync("token"),
        fee:this.data.year
      },
       success: res => {
        var data = res.data.data;
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: 'MD5',
          paySign: data.paySign,
          success (res) {
            wx.showToast({
              title: '支付成功',
              icon:'none',
              duration:2000
            });
            _this.member()
            _this.setData({
              isLogin: App.checkIsLogin()
            });
            // 获取当前用户信息
            _this.getUserDetail();
          },
          fail (res) {
            App.showError('支付失败');
          }
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    _this.member()
    _this.setData({
      isLogin: App.checkIsLogin()
    });
    // 获取当前用户信息
    _this.getUserDetail();
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {}, function(result) {
      _this.setData(result.data);
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})